package com.tem.common.core.base;

import com.tem.common.core.utils.ApiCallUtils;
import com.tem.common.tool.domain.SecurityClaimsUserEntity;
import com.tem.common.tool.domain.SysDataScopeVoEntity;
import com.tem.common.tool.result.ResultException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class BaseController {
    /**
     * 获取分店/企业id
     * @param request
     * @return
     */
    public Long getSysBranchOfficeId(HttpServletRequest request){
        try {
            SecurityClaimsUserEntity securityClaimsUser = ApiCallUtils.securityClaimsUser(request);
            return securityClaimsUser.getSysBranchOfficeId();
        } catch (ResultException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取默认系统
     * @param request
     * @return
     */
    public Long getDefaultSystemLevelId(HttpServletRequest request){
        try {
            SecurityClaimsUserEntity securityClaimsUser = ApiCallUtils.securityClaimsUser(request);
            return securityClaimsUser.getDefaultSystemLevelId();
        } catch (ResultException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 企业id
     * @param request
     * @return
     */
    public Long getSysCompanyId(HttpServletRequest request){
        try {
            SecurityClaimsUserEntity securityClaimsUser = ApiCallUtils.securityClaimsUser(request);
            return securityClaimsUser.getSysCompanyId();
        } catch (ResultException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取全部id
     * @return 0=用户id,1=部门id,2=分店/分公司id,3=企业id,4=系统id
     */
    public Long[] getMyIds(HttpServletRequest request){
        try {
            SecurityClaimsUserEntity securityClaimsUser = ApiCallUtils.securityClaimsUser(request);
            Long[] ids = new Long[5];
            ids[0] = securityClaimsUser.getUserid();
            ids[1] = securityClaimsUser.getDeptId();
            ids[2] = securityClaimsUser.getSysBranchOfficeId();
            ids[3] = securityClaimsUser.getSysCompanyId();
            return ids;
        } catch (ResultException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取角色资源
     * @param request
     * @return
     */
    public List<SysDataScopeVoEntity> getRoles(HttpServletRequest request){
        try {
            SecurityClaimsUserEntity securityClaimsUser = ApiCallUtils.securityClaimsUser(request);
            return securityClaimsUser.getRoles();
        } catch (ResultException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
